package com.yager.service.impl;

import com.yager.mapper.AdminMapper;
import com.yager.model.Admin;
import com.yager.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 12866
 * @version 1.0
 * @date 2021/11/18 9:47
 */
@Slf4j
@Component
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    @Transactional
    public void InsertOneAdmin(Admin admin) {
        log.info("插入一条管理员记录");
        adminMapper.insertSelective(admin);
        return;
    }

    @Override
//    @Cacheable(cacheNames = "admins")
    public List<Admin> getAllAdmin() {

        redisTemplate.setKeySerializer(new StringRedisSerializer());

        List<Admin> list = (List<Admin>) redisTemplate.opsForValue().get("allAdmins");
        System.out.println(list);
        log.info("查询所有管理员信息！");
        if (null == list) {
            list = adminMapper.getAllAdmin();
            System.out.println(list);
            redisTemplate.opsForValue().set("allAdmins", list);
        }

        return list;
    }
}
